const { CreateUser,ModifyInfo,getUsers,getUserById,deleteUserById,ModifyUserRoleById,getUserByName } = require("../service/user_service")
class UserController{
    async addUser(ctx, next) {
        let { username, phone, password } = ctx.request.body;
        await CreateUser(username, phone, password)
        ctx.body = {
            status: 200,
            message:"用户注册成功"
        }
    }
    async modifyUser(ctx,next) {
        let { username, address } = ctx.request.body;
        let { id } = ctx.user
        await ModifyInfo(id, username, address)
        ctx.body = {
            status: 200,
            message:"信息修改成功"
        }
    }
    /**
     * 
     * @param {*} ctx 
     * @param {*} next 
     */
    //后台管理接口
    async getUserList(ctx, next) {
        let res = await getUsers()
        ctx.body = {
            status: 200,
            data: res,
            message:"获取用户数据成功"
        }
    }
    async getUserById(ctx, next) {
        let { id } = ctx.params
        let res = await getUserById(id)
        ctx.body = {
            status: 200,
            data: res,
            message:"获取用户数据成功"
        }
    }
    async modifyById(ctx,next) {
        let { username, address } = ctx.request.body;
        let { id } = ctx.params
        await ModifyInfo(id, username, address)
        ctx.body = {
            status: 200,
            message:"信息修改成功"
        }
    }
    async deleteUser(ctx, next) {
        let { id } = ctx.params
        await deleteUserById(id)
        ctx.body = {
            status: 200,
            message:"注销用户成功"
        }
    }
    //修改用户权限
    async ModifyUserRole(ctx, next) {
        let { id } = ctx.params
        let {role_name}=ctx.request.body
        try {
            await ModifyUserRoleById(id,role_name)
            ctx.body = {
                status: 200,
                message:"修改权限成功"
            }
        } catch (error) {
            console.log(error)
        }
    }
    //搜索用户
    async getUserByName(ctx, next) {
        let {keyValue}=ctx.request.body
        try {
            let userList=await getUserByName(keyValue)
            ctx.body = {
                status: 200,
                message: "搜索成功",
                userList
            }
        } catch (error) {
            console.log(error)
        }
    }
}
module.exports=new UserController()